<template>
  <el-menu
      :default-active="$route.path"
      class="el-menu-vertical-demo"
      background-color="#324157"
      text-color="#fff"
      active-text-color="#20a0ff"
      @open="handleOpen"
      @close="handleClose"
      :collapse="isCollapse"
      :default-openeds="defaultOpeneds">
    <el-menu-item
        v-for="item in noChildren"
        :key="item.path"
        :index="item.path"
        @click="clickMenu(item)">
      <i :class="'el-icon-' + item.icon"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>

    <el-submenu
        v-for="item in hasChildren"
        :key="item.path"
        :index="item.path">
      <template slot="title">
        <i :class="'el-icon-' + item.icon"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item style="color: #a8a8a8;font-weight: bold;"
                    v-for="subItem in item.children"
                    :key="subItem.path"
                    :index="subItem.path"
                    @click="clickMenu(subItem)">
        {{ subItem.label }}
      </el-menu-item>
    </el-submenu>
  </el-menu>
</template>

<script>
export default {
  name: 'CommonAside',
  data() {
    return {
      defaultOpeneds: [],
      menu: [
        {
          path: '/home',
          name: 'Home',
          label: '首页',
          icon: 's-home',
        },
        {
          path: '/notice',
          name: 'Notice',
          label: '通知',
          icon: 'bell',
        },
        {
          path: '/personal',
          name: 'Personal',
          label: '管理员中心',
          icon: 'user',
        },
        {
          path: '/manage',
          label: '管理员管理',
          icon: 's-check',
          children: [
            {
              path: '/manage/admin',
              name: 'admin',
              label: '管理员管理',
            },
            {
              path: '/manage/adminRoles',
              name: 'AdminRoles',
              label: '角色管理',
            },
            {
              path: '/manage/rolesMenu',
              name: 'RolesMenu',
              label: '角色菜单管理',
            },
          ],
        },
        {
          path: '/Article',
          label: '文章管理',
          icon: 'reading',
          children: [
            {
              path: '/Article/ArticleCategory',
              name: 'ArticleCategory',
              label: '文章分类管理',
            },
            {
              path: '/Article/ArticleComment',
              name: 'ArticleComment',
              label: '文章评论管理',
            },
            {
              path: '/Article/ArticleList',
              name: 'ArticleList',
              label: '文章列表管理',
            },
            {
              path: '/Article/ArticleTag',
              name: 'ArticleTag',
              label: '文章标签管理',
            }
          ],
        },
        {
          path: '/UserManage/UserManage',
          name: 'UserManage',
          label: '用户管理',
          icon: 's-custom',
        },
        {
          path: '/ImageManage/Imagemanage',
          name: 'Imagemanage',
          label: '图片管理',
          icon: 'picture-outline',
        },
        {
            path: '/MusicManage/MusicManage',
            name: 'MusicManage',
            label: '音乐管理',
            icon: 's-grid',
        },
      ],
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log('handleOpen key:', key, 'keyPath:', keyPath); // 添加日志输出
    },
    handleClose(key, keyPath) {
      console.log('handleClose key:', key, 'keyPath:', keyPath); // 添加日志输出
    },
    clickMenu(item) {
      console.log('Clicked menu item------:', item);
      this.$router.push({
        name: item.name,
      });
      this.$store.commit('selectMenu', item);

      // 更新当前展开的 submenu
      if (item.children) {
        // 展开当前点击的子菜单，关闭其他子菜单
        this.defaultOpeneds = [item.path];
        console.log('defaultOpeneds:', this.defaultOpeneds)
      } else {
        const parent = this.menu.find(menuItem => menuItem.children && menuItem.children.some(child => child.path === item.path));
        console.log("parent:",parent)
        if (parent) {
          // 展开当前点击的子菜单的父菜单，关闭其他子菜单
          this.defaultOpeneds = [parent.path];
        } else {
          this.defaultOpeneds = [];
        }
      }
    },
  },
  computed: {
    noChildren() {
      return this.menu.filter(item => !item.children);
    },
    hasChildren() {
      return this.menu.filter(item => item.children);
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    },
  },
  watch: {
    '$route.path': {
      immediate: true,
      handler(newPath) {
        console.log('New route path:', newPath); // 添加日志输出
        // 根据路由路径更新默认展开的子菜单
        const foundItem = this.menu.find(item => item.children && item.children.some(child => child.path === newPath));
        if (foundItem) {
          this.defaultOpeneds = [foundItem.path];
        } else {
          this.defaultOpeneds = [];
        }
      },
    },
  },
};
</script>

<style scoped>
.el-menu {
  height: 100%;
  border: none;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
